package com.mvp.shq.washer.ui.orderConfirm;

import com.mvp.shq.washer.entity.Washer;
import com.mvp.shq.washer.entity.result.AliPay;
import com.mvp.shq.washer.entity.result.AlipayResult;
import com.mvp.shq.washer.entity.result.BaseResult;
import com.mvp.shq.washer.entity.OrderResponse;
import com.mvp.shq.washer.entity.result.WasherInfoResult;
import com.mvp.shq.washer.entity.result.WxPayResult;
import com.mvp.shq.washer.http.BaseObserver;
import com.mvp.shq.washer.utils.SpUtil;
import com.mvp.shq.washer.utils.T;

/**
 * Created by Administrator on 2017/8/28.
 */

public class Presenter extends OrderConfirmConstract.Presenter {
    private String token;
    private String userId;
    @Override
    public void onStart() {
        token = SpUtil.getToken();
        userId = SpUtil.getUser().getId();
    }

    @Override
    void getAlipay(String orderNo) {
        model.getSign(token,orderNo)
                .subscribe(new BaseObserver<String>(view) {
                    @Override
                    protected void onSuccees(BaseResult<String> t) throws Exception {
                        if (t.isError()){
                            view.getSignF(t.getMsg());
                        }else {
                            view.getSignS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();
                        view.getSignF("支付宝支付失败");
                    }
                });
    }

    @Override
    void alipay(OrderResponse orderDto) {
        model.alipayPay(token,orderDto)
                .subscribe(new BaseObserver<AliPay>(view) {
                    @Override
                    protected void onSuccees(BaseResult<AliPay> t) throws Exception {
                        if (t.isError()){
                            view.alipayF(t.getMsg());
                        }else {
                            view.alipayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();;
                        view.alipayF(""+e.getMessage());
                    }
                });
    }

    @Override
    void wxPay(OrderResponse orderDto) {
        model.wxPay(token,orderDto)
                .subscribe(new BaseObserver<WxPayResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<WxPayResult> t) throws Exception {
                        if (t.isError()){
                            view.alipayF(t.getMsg());
                        }else {
                            view.wxPayS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        e.printStackTrace();;
                        view.alipayF("未知错误");
                    }
                });
    }

    @Override
    public void getWasherInfo(String washerNo) {
        String token = SpUtil.getToken();
        model.getWasherInfo(token,washerNo)
                .compose(getFragmentLifecycleProvider().bindToLifecycle())
                .subscribe(new BaseObserver<WasherInfoResult>(view) {
                    @Override
                    protected void onSuccees(BaseResult<WasherInfoResult> t) throws Exception {
                        if (t.isError()){
                            T.show(t.getMsg());
                        }else {
                            view.getWasherInfoS(t.getData());
                        }
                    }

                    @Override
                    protected void onFailure(Throwable e, boolean isNetWorkError) throws Exception {
                        T.show("获取洗衣机信息失败");
                    }
                });
    }
}
